import appConfig from '@/config/app.config.js'

/**
 * Tabbar权限控制工具
 * 根据用户权限动态显示不同的tabbar内容
 */

/**
 * 获取用户权限等级
 * @returns {number} 权限等级：0=旧版3个tab（游客默认）, 1=新版5个tab
 */
export function getUserPermissionLevel() {
    try {
        console.log('=== 获取用户权限等级 ===');
        // 从存储中获取权限，如果没有设置则默认为1（新版本，用于测试）
        const level = uni.getStorageSync('userPermissionLevel');
        console.log('从存储获取的权限等级:', level);
        if (level === null || level === undefined || level === '') {
            console.log('权限等级为空，使用默认值1');
            return 0; // 暂时默认使用新版本
        }
        const finalLevel = Number(level);
        console.log('最终权限等级:', finalLevel);
        return finalLevel;
    } catch (error) {
        console.error('获取用户权限失败:', error);
        return 1; // 出错时默认使用新版本
    }
}

/**
 * 获取tabbar配置
 * @returns {object} tabbar配置对象
 */
export function getTabbarConfig() {
    const permission = getUserPermissionLevel();
    
    // 检查是否为demo模式
    const isDemoMode = appConfig.mode.isDemo;
    
    if (permission === 0) {
        // 权限0：原来的3个tab（游客默认）
        return {
            permission: 0,
            theme: 'classic',
            tabs: [
                {
                    text: "首页",
                    pagePath: isDemoMode ? "pages/indexDemo/index" : "pages/oldIndex/index",
                    icon: "home",
                    activeIcon: "home-filled",
                    iconPath: "/static/images/icon_index.png",
                    selectedIconPath: "/static/images/icon_index_active.png"
                },
                {
                    text: "司机福利", 
                    pagePath: "packages/oldpages/pages/commodity2/index",
                    icon: "gift",
                    activeIcon: "gift-filled",
                    iconPath: "/static/images/sb2.png",
                    selectedIconPath: "/static/images/sb.png"
                },
                {
                    text: "会员中心",
                    pagePath: "packages/oldpages/pages/member/index", 
                    icon: "person",
                    activeIcon: "person-filled",
                    iconPath: "/static/images/hy.png",
                    selectedIconPath: "/static/images/icon_hy.png"
                }
            ]
        };
    } else {
        // 权限1：新版5个tab
        return {
            permission: 1,
            theme: 'modern',
            tabs: [
                {
                    text: "首页",
                    pagePath: isDemoMode ? "pages/indexDemo/index" : "pages/indexNew/index",
                    icon: "home",
                    activeIcon: "home-filled",
                    iconPath: "/static/images/shouye.png",
                    selectedIconPath: "/static/images/shouye_n.png"
                },
                {
                    text: "司机生活",
                    pagePath: "pages/driver-life/index-new",
                    icon: "gift",
                    activeIcon: "gift-filled",
                    iconPath: "/static/images/shenghuo.png",
                    selectedIconPath: "/static/images/shenghuo_n.png"
                },
                {
                    text: "扫码",
                    pagePath: "packages/oldpages/pages/scan/index",
                    icon: "scan",
                    activeIcon: "scan",
                    isCenter: true,  // 中间特殊按钮
                    iconPath: "/static/images/tab_scan.png",
                    selectedIconPath: "/static/images/tab_scan_active.png"
                },
                {
                    text: "积分商城",
                    pagePath: "pages/pointsMall/index", // 恢复为订单列表页
                    icon: "shop",
                    activeIcon: "shop-filled",
                    iconPath: "/static/images/shangcheng.png",
                    selectedIconPath: "/static/images/shangcheng_n.png"
                },
                {
                    text: "我的",
                    pagePath: "pages/member/index",
                    icon: "person",
                    activeIcon: "person-filled",
                    iconPath: "/static/images/wode.png",
                    selectedIconPath: "/static/images/wode_n.png"
                }
            ]
        };
    }
}

/**
 * 设置用户权限（用于测试和权限升级）
 * @param {number} level - 权限等级 0或1
 */
export function setUserPermissionLevel(level) {
    try {
        uni.setStorageSync('userPermissionLevel', level);
        console.log(`用户权限已设置为: ${level}`);
    } catch (error) {
        console.error('设置用户权限失败:', error);
    }
}

/**
 * 判断是否使用新版tabbar
 * @returns {boolean} true=新版5个tab, false=旧版3个tab
 */
export function shouldUseNewTabbar() {
    return getUserPermissionLevel() === 1;
}

/**
 * 获取当前页面在tabbar中的索引
 * @param {string} currentRoute - 当前页面路径
 * @returns {number} tabbar索引，未找到返回-1
 */
export function getCurrentTabIndex(currentRoute) {
    const config = getTabbarConfig();
    const index = config.tabs.findIndex(tab => tab.pagePath === currentRoute);
    return index;
}

/**
 * 判断当前页面是否应该显示tabbar
 * @param {string} currentRoute - 当前页面路径
 * @returns {boolean} 是否显示tabbar
 */
export function shouldShowTabbar(currentRoute) {
    console.log('=== 判断是否显示tabbar ===');
    console.log('当前路由:', currentRoute);
    // tabbar应该始终显示，因为只有在需要tabbar的页面中才会引入这个组件
    // pagePath只用于判断当前激活的tab，不影响显示/隐藏
    console.log('tabbar应该显示: true');
    return true;
} 